package com.xhdx.threadpool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadPoolTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // 1. 创建线程池
        ExecutorService pool = Executors.newFixedThreadPool(5);
        List<Future<Integer>> list = new ArrayList<Future<Integer>>();
        for (int i = 0; i < 10; i++) {
            Future<Integer> future = pool.submit(new Callable<Integer>() {
                public Integer call() throws Exception {
                    int sum = 0;
                    for (int i = 0; i <= 100; i++) {
                        sum += i;
                    }
                    //System.out.println(Thread.currentThread().getName() + "正在被执行。sum=" + sum);
                    return sum;
                }
            });
            list.add(future);
        }
//        pool.shutdown();

        for (Future<Integer> future : list) {
            System.out.println(future.get());
        }

//        Future<Integer> future = pool.submit(new Callable<Integer>() {
//            public Integer call() throws Exception {
//                int sum = 0;
//                for (int i = 0; i <= 100; i++) {
//                    sum += i;
//                }
//                return sum;
//            }
//        });
//        System.out.println(future.get());
//        pool.shutdown();
//
        ThreadPoolDemo threadPoolDemo = new ThreadPoolDemo();
        //
        // 2. 为线程池中的线程分配任务
        for (int i = 0; i < 10; i++) {
            pool.submit(threadPoolDemo);
        }
        //
        // 3. 关闭线程池
        pool.shutdown();
        //

        new Thread(threadPoolDemo).start();
        new Thread(threadPoolDemo).start();

    }
}

class ThreadPoolDemo implements Runnable {

    private int i = 0;

    public void run() {
        while (i <= 100) {
            System.out.println(Thread.currentThread().getName() + " " + i++);
        }
    }

}